﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using System.Data;

namespace SharePointListSync.Actions.Controls
{
    public partial class ACListSelectors : UserControl
    {
        public delegate void ProcessDelegate();
        DataManager dManager = new DataManager();

        internal Dictionary<string, string> AllViews = null;
        internal Dictionary<string, string> allCT = null;
        internal Dictionary<string, string> AllColumn = null;
        internal Dictionary<string, string> selectedColumns = null;
        List<string> tempselected = new List<string>();
        internal VOlistDetails mVOlistDetails;


        private bool IscomboBoxViewsChange = false;
        private int comboBoxViewsIndexOld = -1;
        public int comboBoxViewsIndexNew = -1;

        private bool IscomboBoxCTChange = false;
        private int comboBoxCTIndexOld = -1;
        public int comboBoxCTIndexNew = -1;
        public bool IsNavigateBack = false;
        private bool isViewOnly;


        public ACListSelectors()
        {
            InitializeComponent();
        }

        private void ACListSelectors_Load(object sender, EventArgs e)
        {
            selectedColumns = new Dictionary<string, string>();
            AllColumn = new Dictionary<string, string>();
            allCT = new Dictionary<string, string>();
            AllViews = new Dictionary<string, string>();

            ProcessDelegate pre = new ProcessDelegate(Hlp_populateListBox);
            this.Invoke(pre);


        }



        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            radioButton2.Checked = false;
            comboBoxViews.Enabled = true;
            checkedListBoxAllColumn.Enabled = false;
            checkedListBoxAllColumn.Enabled = false;
            
            isViewOnly = true;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            radioButton1.Checked = false;
            isViewOnly = false; 
            checkedListBoxAllColumn.Enabled = true;
            comboBoxViews.Enabled = false;
            checkedListBoxAllColumn.Enabled = true;
                
            if (!IsNavigateBack)
            {
                Hlp_PopulateCheckListBox();
            }
            else
            {
                if(allCT.Count>0)
                {
                allCT.Clear();
                    
                }
                Hlp_PopulateCheckListBox();
                int temp; 
                foreach (string checkedItem in tempselected)
                {
                    temp = checkedListBoxAllColumn.Items.IndexOf(checkedItem);
                    if (temp > 0)
                    {
                        checkedListBoxAllColumn.SetSelected(temp, true);
                        checkedListBoxAllColumn.SetItemChecked(temp, true);
                    }
                }
            }
        }


        private void BtnGetListDetails_Click(object sender, EventArgs e)
        {
            Hlp_resetToNull();


            hlp_ToggelGroupBox(true);

            // get the Selected Value 
            Globals.ThisWorkbook.ActionsPane.UseWaitCursor = true;

            DataRowView dataview = comboBox1.SelectedItem as DataRowView;
            SharePointListDetails.SPConnectionsRow Selectedlist = dataview.Row as SharePointListDetails.SPConnectionsRow;

            Hlp_PopulateContentTypeCombo(Selectedlist);
            Hlp_PopulateAllView(Selectedlist);

            radioButton1.Checked = true; 

            isViewOnly = true; 
            Globals.ThisWorkbook.ActionsPane.UseWaitCursor = false;
        }

        internal void NavigatedBack()
        {

     
            if (!(!isViewOnly && AllColumn != null && AllColumn.Count > 0))
            {
                DataRowView dataview = comboBox1.SelectedItem as DataRowView;
                SharePointListDetails.SPConnectionsRow Selectedlist = dataview.Row as SharePointListDetails.SPConnectionsRow;
                AllColumn.Clear(); 
                AllColumn = dManager.GetListColumns(Selectedlist);
            }
            if (allCT!= null && allCT.Count > 0)
            {
                allCT.Clear();
            }
            if (AllViews != null && AllViews.Count > 0)
            {
                AllViews.Clear(); 
            }
            foreach (string ecah in checkedListBoxAllColumn.CheckedItems)
            {
                tempselected.Add(ecah);
            }
            
            IsNavigateBack = true;

        }


        private void BtnGetData_Click(object sender, EventArgs e)
        {
           
    
            
            DataRowView dataview = comboBox1.SelectedItem as DataRowView;
            SharePointListDetails.SPConnectionsRow listrow = dataview.Row as SharePointListDetails.SPConnectionsRow;

           
                if (isViewOnly)
                {
                    mVOlistDetails = new VOlistDetails(listrow, comboBoxViews.SelectedItem.ToString(), comboBoxCT.SelectedItem.ToString(), selectedColumns, radioButton1.Checked,txtNuOfResult.Text, null);
                    Globals.ThisWorkbook.mACBody.mACListSync.GetDataListView(ref mVOlistDetails);
                }
                else
                {
                    if (selectedColumns != null && selectedColumns.Count < 1)
                    {
                        selectedColumns.Clear();
                    }
                    foreach (string selectItem in checkedListBoxAllColumn.CheckedItems)
                    {
                        selectedColumns.Add(selectItem, AllColumn[selectItem]);
                        selectedColumns.Add("ListItemID", "ID");
                    }
                    mVOlistDetails = new VOlistDetails(listrow, comboBoxViews.SelectedItem.ToString(), comboBoxCT.SelectedItem.ToString(), selectedColumns, radioButton1.Checked, txtNuOfResult.Text,  null);
                    Globals.ThisWorkbook.mACBody.mACListSync.GetDataAllColumns(ref mVOlistDetails);
                }
            
            Globals.ThisWorkbook.hlp_setView(ACControls.ACListSync);
        }

       

        #region Helper Methods

        private void Hlp_populateListBox()
        {

            comboBox1.DataSource = Globals.ThisWorkbook.DSlists.SPConnections;
            comboBox1.DisplayMember = "ConnectionName";
            comboBox1.ValueMember = "ListName";
            hlp_ToggelGroupBox(false);
            //Hlplp_ReSetControls();
        }

        private void Hlp_PopulateAllView(SharePointListDetails.SPConnectionsRow Selectedlist)
        {

            AllViews = dManager.GetAllViews(Selectedlist.ListName, Selectedlist.WebURL);
            foreach (string key in AllViews.Keys)
            {
                comboBoxViews.Items.Add(key);
            }
            comboBoxViews.SelectedIndex = 0;

        }

        private void Hlp_PopulateContentTypeCombo(SharePointListDetails.SPConnectionsRow Selectedlist)
        {   
            comboBoxCT.Items.Add("None");
            allCT = dManager.GetAllContentType(Selectedlist.ListID, Selectedlist.WebURL);
            foreach (string key in allCT.Keys)
            {
                comboBoxCT.Items.Add(key);
            }
            comboBoxCT.SelectedIndex = 0;

        }


        private void Hlp_resetToNull()
        {
            if (mVOlistDetails != null) mVOlistDetails = null;
            if (selectedColumns != null) selectedColumns.Clear();
            if (checkedListBoxAllColumn.Enabled) checkedListBoxAllColumn.ClearSelected();
        }

        public void hlp_ToggelGroupBox(bool p)
        {
            groupBox2.Enabled = p;
            groupBox3.Enabled = p;
        }

        private void Hlp_PopulateCheckListBox()
        {
            if (!(AllColumn != null && AllColumn.Count > 0))
            {
                DataRowView dataview = comboBox1.SelectedItem as DataRowView;
                SharePointListDetails.SPConnectionsRow Selectedlist = dataview.Row as SharePointListDetails.SPConnectionsRow;
                AllColumn = dManager.GetListColumns(Selectedlist);
            }

            foreach (KeyValuePair<string, string> column in AllColumn)
            {
                checkedListBoxAllColumn.Items.Add(column.Key, false);
            }
            checkedListBoxAllColumn.CheckOnClick = true;
        }
        #endregion

        
        
        #region Old Code


        //public ACListSelectors()
        //{
        //    InitializeComponent();
        //}

        //private void ACListSelectors_Load(object sender, EventArgs e)
        //{
        //    //selectedColumns = new Dictionary<string, string>();
        //    //AllColumn = new Dictionary<string, string>();
        //    //allCT = new Dictionary<string, string>();
        //    //AllViews = new Dictionary<string, string>();



        //    ProcessDelegate pre = new ProcessDelegate(populateListBox);
        //    this.Invoke(pre);
        //}

        //private void populateListBox()
        //{
        //    comboBox1.DataSource = Globals.ThisWorkbook.DSlists.SPConnections;
        //    comboBox1.DisplayMember = "ConnectionName";
        //    comboBox1.ValueMember = "ListName";
        //    ToggelGroupBoxEnable(false);
        //    ReSetControls();


        //}

        //private void ReSetControls()
        //{
        //    switch (checkBoxIsView.Checked)
        //    {
        //        case true:

        //            checkedListBoxAllColumn.Enabled = false;
        //            checkedListBoxAllColumn.BackColor = System.Drawing.Color.Gray;
        //            comboBoxViews.Enabled = true;
        //            comboBoxCT.Enabled = true;
        //            resetToNull();

        //            break;

        //        case false:

        //            checkedListBoxAllColumn.Enabled = true;
        //            checkedListBoxAllColumn.BackColor = System.Drawing.Color.White;
        //            comboBoxViews.Enabled = false;
        //            comboBoxCT.Enabled = true;
        //            resetToNull();
        //            break;
        //        default:
        //            break;

        //        //mVOlistDetails.ResetBeforUse();

        //    }

        //}

        //public void ToggelGroupBoxEnable(bool p)
        //{
        //    groupBox2.Enabled = p;
        //    groupBox3.Enabled = p;
        //}


        //#region Navigation - show data in Next Page

        //private void BtnGetData_Click(object sender, EventArgs e)
        //{
        //    Globals.ThisWorkbook.mACBody.mACListSync.Visible = true;
        //    Globals.ThisWorkbook.mACBody.mACListSelector.Visible = false;
        //    Globals.ThisWorkbook.mACBody.mACconnMgr.Visible = false;

        //    // Get the Row details of List . 
        //    DataRowView dataview = comboBox1.SelectedItem as DataRowView;
        //    SharePointListDetails.SPConnectionsRow listrow = dataview.Row as SharePointListDetails.SPConnectionsRow;

        //    if (checkBoxIsView.Checked)
        //    {
        //        mVOlistDetails = new VOlistDetails(listrow, comboBoxViews.SelectedItem.ToString(), comboBoxCT.SelectedItem.ToString(), selectedColumns, checkBoxIsView.Checked, null);
        //        Globals.ThisWorkbook.mACBody.mACListSync.GetDataListView(ref mVOlistDetails);
        //    }

        //    else
        //    {

        //        if (!IsNavigateBack && AllColumn != null && AllColumn.Count < 1)
        //        {
        //            PopulateCheckListBox();

        //        }



        //        foreach (string selectItem in checkedListBoxAllColumn.CheckedItems)
        //        {

        //            selectedColumns.Add(selectItem, AllColumn[selectItem]);
        //        }
        //        mVOlistDetails = new VOlistDetails(listrow, comboBoxViews.SelectedItem.ToString(), comboBoxCT.SelectedItem.ToString(), selectedColumns, checkBoxIsView.Checked, null);
        //        Globals.ThisWorkbook.mACBody.mACListSync.GetDataAllColumns(ref mVOlistDetails);
        //    }
        //}

        //#endregion

        //#region Get List Details

        //private void BtnGetListDetails_Click(object sender, EventArgs e)
        //{
        //    // reset All Variable 

        //    resetToNull();


        //    ToggelGroupBoxEnable(true);

        //    // get the Selected Value 
        //    Globals.ThisWorkbook.ActionsPane.UseWaitCursor = true;

        //    DataRowView dataview = comboBox1.SelectedItem as DataRowView;
        //    SharePointListDetails.SPConnectionsRow Selectedlist = dataview.Row as SharePointListDetails.SPConnectionsRow;

        //    PopulateContentTypeCombo(Selectedlist);
        //    PopulateAllView(Selectedlist);

        //    //checkBoxIsView.Checked = true;


        //    ////mVOlistDetails = new VOlistDetails(Selectedlist, comboBoxViews.SelectedItem.ToString(), comboBoxCT.SelectedItem.ToString(), null, true, null);
        //    ReSetControls();
        //    Globals.ThisWorkbook.ActionsPane.UseWaitCursor = false;
        //}



        //private void PopulateAllView(SharePointListDetails.SPConnectionsRow Selectedlist)
        //{

        //    AllViews = dManager.GetAllViews(Selectedlist.ListName, Selectedlist.WebURL);
        //    foreach (string key in AllViews.Keys)
        //    {
        //        comboBoxViews.Items.Add(key);
        //    }
        //    comboBoxViews.SelectedIndex = 0;
        //}

        //private void PopulateContentTypeCombo(SharePointListDetails.SPConnectionsRow Selectedlist)
        //{
        //    //Dictionary<string, string> allCT = new Dictionary<string, string>(); 
        //    comboBoxCT.Items.Add("None");
        //    allCT = dManager.GetAllContentType(Selectedlist.ListID, Selectedlist.WebURL);
        //    foreach (string key in allCT.Keys)
        //    {
        //        comboBoxCT.Items.Add(key);
        //    }
        //    comboBoxCT.SelectedIndex = 0;
        //}
        //#endregion

        //#region Select Column

        //private void checkBoxISView_CheckedChanged(object sender, EventArgs e)
        //{


        //    if (!checkBoxIsView.Checked)
        //    {


        //        checkedListBoxAllColumn.Enabled = true;
        //        checkedListBoxAllColumn.BackColor = System.Drawing.Color.White;
        //        comboBoxViews.Enabled = false;
        //        checkedListBoxAllColumn.Enabled = true;
        //        PopulateCheckListBox();

        //    }
        //    else
        //    {
        //        if (IscomboBoxCTChange || IscomboBoxViewsChange)
        //        {
        //            comboBoxViews.Enabled = true;
        //            checkedListBoxAllColumn.Enabled = false;
        //            checkedListBoxAllColumn.Enabled = false;
        //            checkedListBoxAllColumn.BackColor = System.Drawing.Color.Gray;
        //        }
        //    }

        //}


        ////TODO: Check for ItemChange ,by a flag  
        //private void PopulateCheckListBox()
        //{
        //    if (!(AllColumn != null && AllColumn.Count > 0))
        //    {
        //        DataRowView dataview = comboBox1.SelectedItem as DataRowView;
        //        SharePointListDetails.SPConnectionsRow Selectedlist = dataview.Row as SharePointListDetails.SPConnectionsRow;
        //        AllColumn = dManager.GetListColumns(Selectedlist);
        //    }

        //    foreach (KeyValuePair<string, string> column in AllColumn)
        //    {
        //        checkedListBoxAllColumn.Items.Add(column.Key, false);
        //    }
        //    checkedListBoxAllColumn.CheckOnClick = true;

        //}


        //private void checkBoxSelectAll_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (checkBoxSelectAll.Checked)
        //    {
        //        checkedListBoxAllColumn.Items.Clear();
        //        foreach (KeyValuePair<string, string> column in AllColumn)
        //        {
        //            checkedListBoxAllColumn.Items.Add(column.Key, true);
        //        }
        //    }
        //    else
        //    {
        //        checkedListBoxAllColumn.Items.Clear();
        //        foreach (KeyValuePair<string, string> column in AllColumn)
        //        {
        //            checkedListBoxAllColumn.Items.Add(column.Value, false);
        //        }
        //    }

        //}

        //private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    resetToNull();
        //    ToggelGroupBoxEnable(false);

        //}
        //#endregion
        //private void resetToNull()
        //{
        //    if (AllViews != null) AllViews.Clear();
        //    if (allCT != null) allCT.Clear();
        //    if (AllColumn != null) AllColumn.Clear();
        //    if (mVOlistDetails != null) mVOlistDetails = null;
        //    if (selectedColumns != null) selectedColumns.Clear();
        //    if (checkedListBoxAllColumn.Enabled) checkedListBoxAllColumn.ClearSelected();
        //}

        //private void comboBoxViews_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (comboBoxViewsIndexOld == -1)
        //    {
        //        comboBoxViewsIndexOld = comboBoxViews.SelectedIndex;
        //        IscomboBoxViewsChange = true;
        //        return;
        //    }

        //    if (comboBoxViews.SelectedIndex != comboBoxViewsIndexNew)
        //    {

        //        if (comboBoxViewsIndexNew == -1)
        //        {
        //            comboBoxViewsIndexNew = comboBoxViews.SelectedIndex;
        //            IscomboBoxViewsChange = true;
        //            return;
        //        }
        //        else
        //        {
        //            comboBoxViewsIndexOld = comboBoxViewsIndexNew;
        //            comboBoxViewsIndexNew = comboBoxViews.SelectedIndex;
        //            IscomboBoxViewsChange = true;
        //            return;
        //        }
        //    }
        //}

        //private void comboBoxCT_SelectedIndexChanged(object sender, EventArgs e)
        //{

        //    if (comboBoxCTIndexOld == -1)
        //    {
        //        comboBoxCTIndexOld = comboBoxCT.SelectedIndex;
        //        IscomboBoxCTChange = true;
        //        return;
        //    }

        //    if (comboBoxCT.SelectedIndex != comboBoxCTIndexNew)
        //    {

        //        if (comboBoxCTIndexNew == -1)
        //        {
        //            comboBoxCTIndexNew = comboBoxCT.SelectedIndex;
        //            IscomboBoxCTChange = true;
        //            return;
        //        }
        //        else
        //        {
        //            comboBoxCTIndexOld = comboBoxCTIndexNew;
        //            comboBoxCTIndexNew = comboBoxCT.SelectedIndex;
        //            IscomboBoxCTChange = true;
        //            return;
        //        }
        //    }

        //}



        ////internal void NavigatedBack()
        ////{

        ////    //comboBoxCT
        ////    //comboBoxViews
        ////    //checkBoxIsView
        ////    //checkedListBoxAllColumn

        ////    switch (checkBoxIsView.Checked)
        ////    {
        ////        case true :
        ////            checkedListBoxAllColumn.Enabled = false;
        ////            comboBoxViews.Enabled = true;
        ////            comboBoxCT.Enabled = true;

        ////            break;

        ////        case false:
        ////            checkedListBoxAllColumn.Enabled = true;
        ////            comboBoxViews.Enabled = false;
        ////            comboBoxCT.Enabled = true;
        ////            resetToNull();
        ////            //mVOlistDetails.ResetBeforUse();

        ////            break;
        ////        default:
        ////            break;
        ////    }



        ////}

        //internal void NavigatedBack()
        //{
        //    if (!(AllColumn != null && AllColumn.Count > 0))
        //    {
        //        DataRowView dataview = comboBox1.SelectedItem as DataRowView;
        //        SharePointListDetails.SPConnectionsRow Selectedlist = dataview.Row as SharePointListDetails.SPConnectionsRow;
        //        AllColumn = dManager.GetListColumns(Selectedlist);

        //    }




        //} 
        #endregion








    }
}
